﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using Newtonsoft.Json.Converters;
using ServerPicture.Models;

namespace ServerPicture.Controllers
{
    public class PictureController : ApiController
    {

        /// <summary>                                                                       
        /// GET PHOTO BY ID PHOTO
        /// </summary>
        /// <returns></returns>
        [Route("api/Picture/GetPhotoByID/{PhotoID:int}")]
        [HttpGet]
        public List<PhotoModels> LoadPhotoByID(int PhotoID)
        {
            DatabaseDataContext db = new DatabaseDataContext();
            var list = (from p in db.Photos
                        where p.Photo_ID == PhotoID
                        select new Models.PhotoModels
                        {
                            _photo_ID = p.Photo_ID,
                            _photo_des = p.Photo_Description,
                            _photo_date_create = string.Format("{0:dd/MM/yyyy}", p.Photo_DateAdd),
                            _photo_date_update = string.Format("{0:dd/MM/yyyy}", p.Photo_DateUpdate),
                            _photo_id_cate = p.Cate_ID,
                            _photo_url = p.URL

                        }).ToList();

            if (list == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return list;
        }
       
        /// <summary>
        /// GET PHOTO OF CATE
        /// </summary>
        /// <returns></returns>
        [Route("api/Picture/GetPhotoByCate/Cate/{CateId:int}/Page/{Page:int}")]
        [HttpGet]
        public List<PhotoModels> LoadPhotoOfCate(int cateid,int Page )
        {
            DatabaseDataContext db = new DatabaseDataContext();
            var list = (from p in db.Photos where p.Cate_ID==cateid
                        orderby p.Photo_ID descending
                        select new Models.PhotoModels
                        {
                            _photo_ID = p.Photo_ID,
                            _photo_des = p.Photo_Description,
                            _photo_date_create = string.Format("{0:dd/MM/yyyy}", p.Photo_DateAdd),
                            _photo_date_update = string.Format("{0:dd/MM/yyyy}", p.Photo_DateUpdate),
                            _photo_id_cate = p.Cate_ID,
                            _photo_url = p.URL,
                            _photo_url_thumbal = p.URLThumbal

                        }).Skip(Page*18).Take(18).ToList();

            if (list == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return list;
        }
        
        /// <summary>
        /// GET PHOTO OF Tag
        /// </summary>
        /// <returns></returns>
        [Route("api/Picture/GetPhotoByTag/Tag/{TagID:int}/Page/{Page:int}")]
        [HttpGet]
        public List<PhotoModels> LoadPhotoOfTag(int tagid, int Page)
        {
            DatabaseDataContext db = new DatabaseDataContext();
            var list = (from p in db.TagCouples
                        where p.TagID==tagid
                        orderby p.Tag.Tag_Name descending
                        select new Models.PhotoModels
                        {
                            _photo_ID = p.Photo.Photo_ID,
                            _photo_des = p.Photo.Photo_Description,
                            _photo_date_create = string.Format("{0:dd/MM/yyyy}", p.Photo.Photo_DateAdd),
                            _photo_date_update = string.Format("{0:dd/MM/yyyy}", p.Photo.Photo_DateUpdate),
                            _photo_id_cate = p.Photo.Category.Cate_ID,
                            _photo_url = p.Photo.URL,
                            _photo_url_thumbal = p.Photo.URLThumbal
                                                                                               
                        }).Skip(Page * 18).Take(18).ToList();

            if (list == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return list;
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [Route("api/Picture/GetPhotoByType/Type/{type:int}/Page/{page:int}/")]
        [HttpGet]
        public List<PhotoModels> LoadPhotoFirst(int type,int Page)
        {
            List<PhotoModels> list = new List<PhotoModels>();
            DatabaseDataContext db = new DatabaseDataContext();
            if(type==1)
            {
                var list1 = (from p in db.Photos
                            orderby p.DownNumber descending
                            select new Models.PhotoModels
                            {
                                _photo_ID = p.Photo_ID,
                                _photo_des = p.Photo_Description,
                                _photo_date_create = string.Format("{0:dd/MM/yyyy}", p.Photo_DateAdd),
                                _photo_date_update = string.Format("{0:dd/MM/yyyy}", p.Photo_DateUpdate),
                                _photo_id_cate = p.Category.Cate_ID,
                                _photo_url = p.URL,
                                _photo_download = p.DownNumber,
                                _photo_url_thumbal = p.URLThumbal

                            }).Skip(Page * 18).Take(18).ToList();
                list = list1;
            }
            else
            {
                var list2 = (from p in db.Photos
                            orderby p.Photo_ID descending
                            select new Models.PhotoModels
                            {
                                _photo_ID = p.Photo_ID,
                                _photo_des = p.Photo_Description,
                                _photo_date_create = string.Format("{0:dd/MM/yyyy}", p.Photo_DateAdd),
                                _photo_date_update = string.Format("{0:dd/MM/yyyy}", p.Photo_DateUpdate),
                                _photo_id_cate = p.Category.Cate_ID,
                                _photo_url = p.URL,
                                _photo_download = p.DownNumber,
                                _photo_url_thumbal = p.URLThumbal

                            }).Skip(Page * 18).Take(18).ToList();
                list = list2;
            }
          

            if (list == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return list;
        }
        [Route("api/Picture/Search/{searchkey}/{page}")]
        [HttpGet]
        public List<Models.PhotoModels> SearchPictureByName(string searchkey, int page)
        {
            DatabaseDataContext db = new DatabaseDataContext();
            var list = (from p in db.Photos
                        where p.Photo_Description.ToLower().Trim().Contains(searchkey.ToLower().Trim())
                        select new Models.PhotoModels
                        {
                            _photo_ID = p.Photo_ID,
                            _photo_des = p.Photo_Description,
                            _photo_date_create = string.Format("{0:dd/MM/yyyy}", p.Photo_DateAdd),
                            _photo_date_update = string.Format("{0:dd/MM/yyyy}", p.Photo_DateUpdate),
                            _photo_id_cate = p.Category.Cate_ID,
                            _photo_url = p.URL,
                            _photo_url_thumbal = p.URLThumbal

                        }).Skip(page * 18).Take(18).ToList();

            if (list == null)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            return list;
        }
        
           

        
      
       

    }
}
